package chixing.day19.work2;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TaskManager {
    Map<String, Task> taskMap = new HashMap<>();
    Map<String, List<LocalDateTime>> taskRecord = new HashMap<>();
    public  void registerTask(Task task)
    {
        taskMap.put(task.getName(), task);
        taskRecord.putIfAbsent(task.getName(), new ArrayList<>());
    }
    public Task getTaskByName(String name)
    {
        return taskMap.get(name);
    }
    public TaskManager() {
        taskRecord.put("SyncStockTask", null);
        taskRecord.put("SendInterviewTask", null);
        taskRecord.put("GenerateReportTask", null);
    }
    public void executeTask(String name) {
        Task task = getTaskByName(name);
            task.execute();
            LocalDateTime now = LocalDateTime.now();
            taskRecord.get(name).add(now);
    }
    public void printHistory(String name) {
        List<LocalDateTime> history = taskRecord.get(name);
        System.out.println("任务 " + name + " 的执行历史：");
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        for (LocalDateTime time : history) {
            System.out.println(time.format(formatter));
        }
    }
}
